package org.springframework.fusion.webscoket.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.fusion.webscoket.config.websocket.WebSocketInterceptor;
import org.springframework.fusion.webscoket.config.websocket.WebSocketInterceptor2;
import org.springframework.fusion.webscoket.config.websocket.WebSocketMessageClass;
import org.springframework.fusion.webscoket.config.websocket.WebSocketMessageClass2;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
	
	/**
	 * 设置连接地址和拦截器
	 */
	@Override
	public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
	    registry.addHandler(webSocketMessage(), "/ws1").setAllowedOrigins("*").addInterceptors(new WebSocketInterceptor());
	    registry.addHandler(webSocketMessage2(), "/ws2").setAllowedOrigins("*").addInterceptors(new WebSocketInterceptor2());
	}

	/**
	 * 配置消息处理器
	 */
	@Bean
	public WebSocketMessageClass webSocketMessage() {
		WebSocketMessageClass webSocketMessage = new WebSocketMessageClass();
		return webSocketMessage;
	}
	
	/**
	 * 配置消息处理器
	 */
	@Bean
	public WebSocketMessageClass2 webSocketMessage2() {
		WebSocketMessageClass2 webSocketMessage2 = new WebSocketMessageClass2();
		return webSocketMessage2;
	}

	/**
	 * 配置消息缓冲时间和session的超市时间
	 */
	@Bean
	public ServletServerContainerFactoryBean servletServerContainerFactoryBean() {
		ServletServerContainerFactoryBean servletServerContainerFactoryBean = new ServletServerContainerFactoryBean();
		servletServerContainerFactoryBean.setMaxTextMessageBufferSize(8192);
		servletServerContainerFactoryBean.setMaxBinaryMessageBufferSize(8192);
		servletServerContainerFactoryBean.setMaxSessionIdleTimeout(1200000L);
		return servletServerContainerFactoryBean;
	}

}
